//
//  RegisterViewController.m
//  YiLiao
//
//  Created by Infinite on 16/7/1.
//  Copyright © 2016年 Infinite. All rights reserved.
//

#import "RegisterViewController.h"
#import <AFNetworking.h>



@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *UserName;
@property (weak, nonatomic) IBOutlet UITextField *PassWord;
@property (weak, nonatomic) IBOutlet UITextField *TurePW;
@property (weak, nonatomic) IBOutlet UITextField *PickName;
@property (weak, nonatomic) IBOutlet UITextField *Email;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"注册";
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)registerAction:(id)sender {
    
    //谓词判断邮箱
    NSString * email = self.Email.text;
    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isValid = [predicate evaluateWithObject:email];
    
    if (self.UserName.text.length ==0 || self.PassWord.text.length == 0 || self.TurePW.text.length == 0) {
        NSLog(@"某行缺少输入");
    }else if (isValid == NO){
        NSLog(@"邮箱格式不对");
    }else if (![self.PassWord.text isEqualToString:self.TurePW.text]){
        NSLog(@"前后密码不同");
    }else{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/html",nil]];
    
    NSDictionary * parameters =  @{@"command":@"ST_R",@"name":self.UserName.text,@"psw":self.PassWord.text,@"nickname":self.PickName.text,@"email":self.Email.text,};
    [manager POST:@"http://localhost:8080/st/s" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"json:%@",responseObject);
        
        NSDictionary *infoDic = responseObject;
        if ([[infoDic objectForKey:@"result"] isEqualToString:@"0"]) {
            NSLog(@"用户已经存在");
        }else if ([[infoDic objectForKey:@"result"] isEqualToString:@"1"]){
            NSLog(@"成功注册");
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
    }];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
